/**
 * JAVACC DEMO 1.0
 */
package com.apache.portal.common.template.helper;

import com.apache.cache.util.Validator;
import com.apache.info.entity.PubMetadata;
import com.apache.portal.common.oscache.BaseOsCache;
import com.apache.portal.common.oscache.OsCacheOtherManager;
import com.apache.portal.common.template.FreemarkerHelper;
import com.apache.portal.common.util.FileOperate;
import com.apache.portal.thread.CreateParamManagerInfoJob;
import com.apache.tools.DateUtils;
import com.apache.uct.common.ToolsUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:  生成页面实现类
 *
 * @author dyhou 创建时间：2016
 */
public class PageTempManager extends SupperTemplateManager {

    public boolean createPage(Map<String, Object> param) {
        return newMethod(param);
    }

    private boolean newMethod(Map<String, Object> param) {
        boolean mark = false;
        List<Map> list = (List<Map>) param.get("sortList");
        //String prefix = String.valueOf(param.get("projectName")) + "/pages/";
        String root = ToolsUtil.getInstance().getLocalByKey("config.properties","jsp_path");
        if (!root.endsWith("/"))
            root += "/";
        String xmlPath = root + String.valueOf(param.get("filePath")) + "/";
        Map<String, Object> data = new HashMap<String, Object>();
        try {
            PubMetadata metadata = new PubMetadata();
            FileOperate.getInstance().newCreateFolder(xmlPath);
            metadata.setSortId("param_manager");
            Map<String, Object> paramVo = new HashMap<String, Object>();
            paramVo.put("sysParamManager.w_sortId", "param_manager");
            paramVo.put("modelTypes", "s_sysParamManager");
            List<Map> pms = (List) init(paramVo);
            for (Map map : list) {
                String reqObjName = String.valueOf(map.get("objName"));
                data.put("objName", reqObjName);
                data.put("sort", map);
                paramVo.remove("sysParamManager.w_sortId");
                paramVo.put("metadata.w_sortId", String.valueOf(map.get("sortId")));
                paramVo.put("modelTypes", "s_metadata");
                List mts = (List) init(paramVo);
                paramVo.remove("metadata.w_sortId");
                data.put("metadatas", mts);
                data.put("projectName", param.get("projectName"));
                for (int i = 0; i < mts.size(); i++) {
                    Map mtp = (Map) mts.get(i);
                    if ("1".equals(String.valueOf(mtp.get("dataRestrict")))) {
                        data.put("primaryKeyId", String.valueOf(mtp.get("dataAttr")));
                        data.put("dataPrimaryKey", String.valueOf(mtp.get("dataAttr")));
                    }
                }

                String templateDefaultName = "coding/pages/objName-list.inc";
                String templateName = Validator
                        .getDefaultStr(String.valueOf(param.get("templateName")),
                                templateDefaultName);
                String result = FreemarkerHelper.instance().getTemplateStr(data, templateName);
                result = result.replace("###", "$");
                FileOperate.getInstance().newCreateFile(xmlPath + reqObjName + "-list.jsp", result);

                //往paramManager表中插入相关配置。

                String modelTypes[] = { "i_" + reqObjName, "u_" + reqObjName, "s_" + reqObjName };
                String FormNames[] = { "addForm", "editForm", "searchForm" };
                //ParamsVo pvo = new ParamsVo();
                BaseOsCache oscache = OsCacheOtherManager.getInstance()
                        .getBaseOsCache("param_manager_data_", 60 * 60 * 24);
                for (int i = 0; i < FormNames.length; i++) {
                    //如果之前已经存在相关配置，则不需要生成。
                    String pageName = reqObjName + "-list";//TODO 未来应该提到外面，当参数传过来。
                    String formName = FormNames[i];
                    String key = formName + "_" + pageName;
                    Object obj = oscache.get(key);
                    if (ToolsUtil.isEmpty(obj)) {
                        Map<String, String> pmMap = new HashMap<String, String>();
                        paramVo.put("sysParamManager.modelTypes", modelTypes[i]);
                        paramVo.put("sysParamManager.formName", formName);
                        paramVo.put("sysParamManager.delStatus", "F");
                        paramVo.put("sysParamManager.createTime",
                                DateUtils.Now.fmt_yyyyMMdd_HHmmss());
                        paramVo.put("sysParamManager.pageName", pageName);
                        paramVo.put("modelTypes", "i_sysParamManager");
                        initSave(paramVo);
                    }
                }
                CreateParamManagerInfoJob.getInstance().initParamManagerInfo();
            }
            mark = true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("生成页面失败", e);
        }
        return mark;
    }

    private boolean defaultMethod(Map<String, Object> param) {
        boolean mark = false;
        List<Map> list = (List<Map>) param.get("sortList");
        String prefix = String.valueOf(param.get("projectName")) + "/pages/";
        String xmlPath = String.valueOf(param.get("filePath")) + "/" + prefix;
        //		String sysName = jarPath.substring(jarPath.lastIndexOf(".") + 1);//com.iussoft.utr中的utr
        String sysName = String.valueOf(param.get("sysCode"));
        Map<String, Object> data = new HashMap<String, Object>();
        try {
            PubMetadata metadata = new PubMetadata();
            FileOperate.getInstance().newCreateFolder(xmlPath);
            FileOperate.getInstance().newCreateFolder(xmlPath + "jsp/");
            metadata.setSortId("param_manager");
            Map<String, Object> paramVo = new HashMap<String, Object>();
            paramVo.put("sysParamManager.w_sortId", "param_manager");
            paramVo.put("modelTypes", "s_sysParamManager");
            List<Map> pms = (List) init(paramVo);
            for (Map map : list) {
                String reqObjName = String.valueOf(map.get("objName"));
                data.put("objName", reqObjName);
                data.put("sysName", sysName);
                data.put("sort", map);
                paramVo.remove("sysParamManager.w_sortId");
                paramVo.put("metadata.w_sortId", String.valueOf(map.get("sortId")));
                paramVo.put("modelTypes", "s_metadata");
                List mts = (List) init(paramVo);
                paramVo.remove("metadata.w_sortId");
                data.put("metadatas", mts);
                data.put("projectName", param.get("projectName"));
                for (int i = 0; i < mts.size(); i++) {
                    Map mtp = (Map) mts.get(i);
                    if ("1".equals(String.valueOf(mtp.get("dataRestrict")))) {
                        data.put("primaryKeyId", String.valueOf(mtp.get("dataAttr")));
                        data.put("dataPrimaryKey", String.valueOf(mtp.get("dataAttr")));
                    }
                }
                //				String result = FreemarkerHelper.instance().getTemplateStr("views/objName-edit-html.inc", data);
                //				result = result.replace("###", "$");
                //				FileOperate.getInstance().newCreateFile(xmlPath + "common/" + reqObjName + "-edit.html", result);
                //				result = FreemarkerHelper.instance().getTemplateStr("views/objName-list-html-inc", data);
                //				FileOperate.getInstance().newCreateFile(xmlPath + "common/" + reqObjName + "-list.html", result);
                //				result = FreemarkerHelper.instance().getTemplateStr("views/objName-view-html.inc", data);
                //				FileOperate.getInstance().newCreateFile(xmlPath + "common/" + reqObjName + "-view.html", result);
                String result = FreemarkerHelper.instance()
                        .getTemplateStr(data, "coding/pages/objName-list.inc");
                result = result.replace("###", "$");
                FileOperate.getInstance().newCreateFile(xmlPath + reqObjName + "-list.jsp", result);

                //往paramManager表中插入相关配置。

                String modelTypes[] = { "i_" + reqObjName, "u_" + reqObjName, "s_" + reqObjName };
                String FormNames[] = { "addForm", "editForm", "searchForm" };
                //ParamsVo pvo = new ParamsVo();
                BaseOsCache oscache = OsCacheOtherManager.getInstance()
                        .getBaseOsCache("param_manager_data_", 60 * 60 * 24);
                for (int i = 0; i < FormNames.length; i++) {
                    //如果之前已经存在相关配置，则不需要生成。
                    String pageName = reqObjName + "-list";//TODO 未来应该提到外面，当参数传过来。
                    String formName = FormNames[i];
                    String key = formName + "_" + pageName;
                    Object obj = oscache.get(key);
                    if (ToolsUtil.isEmpty(obj)) {
                        Map<String, String> pmMap = new HashMap<String, String>();
                        paramVo.put("sysParamManager.modelTypes", modelTypes[i]);
                        paramVo.put("sysParamManager.formName", formName);
                        paramVo.put("sysParamManager.delStatus", "F");
                        paramVo.put("sysParamManager.createTime",
                                DateUtils.Now.fmt_yyyyMMdd_HHmmss());
                        paramVo.put("sysParamManager.pageName", pageName);
                        paramVo.put("modelTypes", "i_sysParamManager");
                        initSave(paramVo);
                    }
                    //					Map<String, Object> paramManagerVo = new HashMap<String, Object>();
                    //					paramManagerVo.put("modelTypes", "s_sysParamManager");
                    //					paramManagerVo.put("sysParamManager.w_pageName", pageName);
                    //					paramManagerVo.put("sysParamManager.formName", formName);
                    //					List<Map> paramManagerList = (List) init(paramManagerVo);
                    //					if (paramManagerList.size() > 0) {
                    //						//如果存在值则不再插入
                    //					} else {
                    //						//不存在值则插入
                    //						Map<String, String> pmMap = new HashMap<String, String>();
                    //						paramVo.put("sysParamManager.modelTypes", modelTypes[i]);
                    //						paramVo.put("sysParamManager.formName", formName);
                    //						paramVo.put("sysParamManager.delStatus", "F");
                    //						paramVo.put("sysParamManager.createTime", DateUtils.Now.fmt_yyyyMMdd_HHmmss());
                    //						paramVo.put("sysParamManager.pageName", pageName);
                    //						paramVo.put("modelTypes", "i_sysParamManager");
                    //						initSave(paramVo);
                    //					}

                }
                CreateParamManagerInfoJob.getInstance().initParamManagerInfo();
            }
            mark = true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error("生成页面失败", e);
        }
        return mark;
    }

}
